import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_ui/src/components/ListItem.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

import '../FormCreator.dart';

class FormImagePicker extends StatefulWidget {
  final FormControlContext controlContext;
  FormImagePicker({Key key, this.controlContext}) : super(key: key);

  _FormImagePickerState createState() => _FormImagePickerState();
}

class _FormImagePickerState extends State<FormImagePicker> {
  File pickImage;
  _pickImage(String value) {
    FormCreatorHandler formHandler = FormCreatorState.of(context);
    formHandler.changeValue(widget.controlContext.copyWith(context: context, value: value));
  }

  Future<String> _showImagePicker(context, String currentValue, crop) async {
    return showModalBottomSheet<String>(
      builder: (BuildContext _context) {
        return new Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            new ListTile(
              leading: new Icon(Icons.photo_camera),
              title: new Text("相机"),
              onTap: () async {
                pickImage = await ImagePicker.pickImage(source: ImageSource.camera);
                if(crop) {
                  pickImage = await ImageCropper.cropImage(
                    sourcePath: pickImage.path,
                    ratioX: 1.0,
                    ratioY: 1.0,
                    maxWidth: 512,
                    maxHeight: 512,   
                  );
                }
                Navigator.pop(context, pickImage?.path);
              },
            ),
            new ListTile(
              leading: new Icon(Icons.photo_library),
              title: new Text("相册"),
              onTap: () async {
                pickImage = await ImagePicker.pickImage(source: ImageSource.gallery);
                if(crop) {
                  pickImage = await ImageCropper.cropImage(
                    sourcePath: pickImage.path,
                    ratioX: 1.0,
                    ratioY: 1.0,
                    maxWidth: 512,
                    maxHeight: 512,   
                  );
                }
                Navigator.pop(context, pickImage?.path);
              },
            ),
            new ListTile(
              leading: new Icon(Icons.cancel),
              title: new Text("取消"),
              onTap: () async {
                Navigator.pop(context, currentValue);
              },
            ),
          ],
        );
      }, context: context
    );
  }
  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> props = widget.controlContext.mergedProps;
    // FormCreatorHandler formHandler = FormCreatorState.of(context);
    dynamic value = widget.controlContext.value;
    bool crop = props["crop"] ??= false;

    dynamic image = value == null ? Text(props["placeholder"], textAlign: TextAlign.end) : 
    Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: <Widget>[
        CircleAvatar(
          backgroundImage: FileImage(File(value)),
          radius: 24,
        )
      ]
    );
    return ListItem(
      title: props["title"] ??= "图片选择",
      arrow: true,
      // horizontal: false,
      child: image,
      onTap: ()  async {
        String pickImagePath = await _showImagePicker(context, value, crop);
        print(pickImagePath);
        // TODO: 需要上传图片
        _pickImage(pickImagePath);
      }
    );
  }
}